using System;
using System.Collections.Generic;
using System.Text;

namespace MSRI.HelperClasses
{
    public enum MessageTypeEnum { Sms, Command, DeliveryReport, UnKnown };
    public enum CommandTypeEnum { StartFilter, StopFilter, CloseApplication, SetValue, GetInboxMessage };
    public enum ResponseClassEnum { Multiple, Single, Ignore };
    public enum ResponseSessionStateEnum { Reset, Delete, Ignore, Error };
    public enum DeliveryStatusEnum { Sent, Failed };
    public enum Delim { Field = 1, Packet = 2 };
    public enum Constants { MaxMessageLength = 4096 };
    [Serializable()]
    public class SmsClass
    {
        private string _Number;
        private string _Body;
        private DateTime _Time;
        private Guid _MessageID;
        private string _clientName;

        public string Number
        {
            get { return _Number; }
            set { _Number = value; }
        }

        public string ClientName
        {
            get { return _clientName; }
            set { _clientName = value; }
        }

        public string Body
        {
            get { return _Body; }
            set { _Body = value; }
        }

        public DateTime Time
        {
            get { return _Time; }
            set { _Time = value; }
        }

        public Guid MessageID
        {// This should be unique 
            get { return _MessageID; }
            set { _MessageID = value;}
        }

        public SmsClass()
        {
            _Number = "";
            _Body = "";
            _Time = DateTime.Now;
            _MessageID = Guid.NewGuid();
            _clientName = null;
        }

        public SmsClass(string MobileNumber, String SmsBody)
        {
            _Number = MobileNumber;
            _Body = SmsBody;
            _MessageID = Guid.NewGuid();
            _Time = DateTime.Now;
            _clientName = null;
        }

        public SmsClass(SmsClass SmsMessage)
        {
            _Number = SmsMessage.Number;
            _Body = SmsMessage.Body;
            _Time = SmsMessage.Time;
            _MessageID = SmsMessage.MessageID;
            _clientName = SmsMessage.ClientName;
        }

        public SmsClass(string[] Fields)
        {
            _Number = Fields[1];
            _Body = Fields[2];
            _Time = new DateTime(Convert.ToInt64(Fields[3]));
            _MessageID = new Guid(Fields[4]);
        }

        public int Sizeof()
        {
            int length = 0;
            length += _Number.ToString().Length + 1;
            length += _Body.Length + 1;
            length += _Time.ToString().Length + 1;
            length += _MessageID.ToString().Length + 1;
            return length;
        }

        public override string ToString()
        {

            String str = ((int)MessageTypeEnum.Sms).ToString() + Convert.ToChar(Delim.Field) +
                _Number.ToString() + Convert.ToChar(Delim.Field) +
                _Body.ToString() + Convert.ToChar(Delim.Field) +
                _Time.Ticks.ToString() + Convert.ToChar(Delim.Field) +
                _MessageID.ToString();
            return str;
        }

    }

    [Serializable()]
    public class CommandClass
    {
        private CommandTypeEnum _Type;
        private string _Name;
        private string _Value;

        public CommandTypeEnum Type
        {
            get { return _Type; }
            set { _Type = value; }
        }

        public string Name
        {
            get { return _Name; }
            set { _Name = value; }
        }

        public string Value
        {
            get { return _Value; }
            set { _Value = value; }
        }

        public CommandClass()
        {
            _Name = "";
            _Value = "";
            _Type = CommandTypeEnum.SetValue;
        }

        public CommandClass(string[] Fields)
        {
            _Name = Fields[1];
            _Value = Fields[2];
            _Type = (CommandTypeEnum)Convert.ToInt16(Fields[3]);
        }
        public override string ToString()
        {
            String str = ((int)MessageTypeEnum.Command).ToString() + Convert.ToChar(Delim.Field) +
                _Name.ToString() + Convert.ToChar(Delim.Field) +
                _Value.ToString() + Convert.ToChar(Delim.Field) +
                ((int)_Type).ToString();
            return str;
        }
    }

    [Serializable()]
    public class DeliveryReportClass
    {
        private Guid _MessageID;
        private DeliveryStatusEnum _Status;

        public Guid MessageID
        {
            get { return _MessageID; }
            set { _MessageID = value; }
        }

        public DeliveryStatusEnum Status
        {
            get { return _Status; }
            set { _Status = value; }
        }

        public DeliveryReportClass()
        {
            _Status = DeliveryStatusEnum.Sent;
            _MessageID = Guid.NewGuid();
        }
        public DeliveryReportClass(string[] Fields)
        {
            Status = (DeliveryStatusEnum)Convert.ToInt16(Fields[1]);
            MessageID = new Guid(Fields[2]);
        }
        public DeliveryReportClass(DeliveryReportClass DeliveryReport)
        {
            _MessageID = DeliveryReport.MessageID;
            _Status = DeliveryReport.Status;
        }
        public override string ToString()
        {
            String str = ((int)MessageTypeEnum.DeliveryReport).ToString() + Convert.ToChar(Delim.Field) +
                ((int)Status).ToString() + Convert.ToChar(Delim.Field) +
                MessageID.ToString();
            return str;
        }
    }

    [Serializable]
    public class SmsResponseClass
    {
        public List<SmsClass> ResponseMessages;
        private ResponseSessionStateEnum _sessionState;

        public SmsResponseClass()
        {
            ResponseMessages = new List<SmsClass>();
            _sessionState = ResponseSessionStateEnum.Ignore;
        }

        public SmsResponseClass(SmsClass SmsItem)
        {
            ResponseMessages = new List<SmsClass>();
            ResponseMessages.Add(SmsItem);
            _sessionState = ResponseSessionStateEnum.Ignore;
        }

        public SmsResponseClass(SmsResponseClass Item)
        {
            ResponseMessages = new List<SmsClass>(Item.ResponseMessages);
            _sessionState = Item.SessionState;
        }

        public void AddResponseSms(SmsClass SmsItem)
        {
            if(ResponseMessages==null)
                ResponseMessages= new List<SmsClass>();
            ResponseMessages.Add(SmsItem);
        }

        public int Count
        {
            get 
            {
                if (ResponseMessages == null || ResponseMessages.Count == 0)
                    return 0;
                else
                    return ResponseMessages.Count;
            }
        }
        public ResponseClassEnum Type
        {
            get 
            {
                if (ResponseMessages == null || ResponseMessages.Count == 0)
                    return ResponseClassEnum.Ignore;
                else if (ResponseMessages.Count == 1)
                    return ResponseClassEnum.Single;
                else
                    return ResponseClassEnum.Multiple;
            }
        }

        public SmsClass this[int index]
        {
            get
            {
                if (ResponseMessages != null && ResponseMessages.Count > index)
                    return ResponseMessages[index];
                return null;
            }
        }
        public ResponseSessionStateEnum SessionState
        {
            get { return _sessionState; }
            set { _sessionState = value; }
        }

        
    }



}
